package com.zhkjnet.zhyy.job;

import com.zhkjnet.zhyy.entity.Asset;
import com.zhkjnet.zhyy.entity.RechargeRecord;
import com.zhkjnet.zhyy.service.AssetService;
import com.zhkjnet.zhyy.service.RechargeRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 返现定时任务
 */
@Component
public class CashbackJob {

    private final static Logger logger = LoggerFactory.getLogger(CashbackJob.class);

    @Autowired
    private AssetService assetService;

    @Autowired
    private RechargeRecordService rechargeRecordService;

    /**
     * 每天固定返可用资金
     * 每晚3点执行
     */
    //@Scheduled(cron = "0 */10 * * * ?")
    @Scheduled(cron = "0 0 3 * * ?")
    public void fixed() {
        logger.info("execute cashback job");
        List<Asset> assets = assetService.findAllCashbackAsset();
        for (Asset asset : assets) {
            assetService.cashback(asset);
        }
    }

    /**
     *
     * 单笔10%返现
     * 每晚1点执行
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void single() {
        logger.info("execute single job");
        List<RechargeRecord> records = rechargeRecordService.findAllSingle();
        for (RechargeRecord record : records) {
            assetService.single(record);
        }
    }

    /**
     * 检查是否到最低金额
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void checkMin() {
        logger.info("execute check min job");
        rechargeRecordService.updateAllMin();
    }

}
